<?php
/**
 * @file: Unreadm.php
 * @User: shiwuhao
 * @Date: 16/5/23 14:37
 * @synopsis: 未读消息提示
 */

class Unreadm extends CI_Model
{
    // 需要加入小红点提示的帖子类别
    private $need_post_type_extend = array(1,2,3);

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 更新 科室/标签 下的最新帖子 ID
     * @param $post_id
     * @param $post_type_extend
     * @param array $thread_ids
     * @param array $tag_ids
     * @return bool
     */
    public function update_unread_post_id($post_id, $post_type_extend, $thread_ids = array(), $tag_ids = array())
    {
        if (!$post_id || !in_array($post_type_extend, $this->need_post_type_extend)) return false;

        if ($post_type_extend == 2 || $post_type_extend == 3) $post_type_extend = '2_3';

        if (!empty($thread_ids)) { // 科室
            foreach ($thread_ids as $v) {
                $key = THREAD_UNREAD_PREFIX . $v . ':' . $post_type_extend;
                $res = $this->redis->save($key, $post_id, THREAD_UNREAD_EXPIRE_TIME);
            }
        }

        if (!empty($tag_ids)) {
            foreach ($tag_ids as $v) { // 标签
                $key = TAG_UNREAD_PREFIX . $v . ':' . $post_type_extend;
                $res = $this->redis->save($key, $post_id, THREAD_UNREAD_EXPIRE_TIME);
            }
        }

        return true;
    }

    /**
     * 更新用户类别下 已读的最大帖子ID
     * @param $user_id
     * @param $post_type_extend
     */
    public function update_user_read_max_post_id($user_id, $post_id, $post_type_extend = 0)
    {
        if (!$user_id || !in_array($post_type_extend, $this->need_post_type_extend)) return false;

        if ($post_type_extend == 2 || $post_type_extend == 3) $post_type_extend = '2_3';

        $key = USER_INFO_PREFIX.$user_id;
        $hash_key = 'category_'.$post_type_extend;

        $curr_post_id = (int)$this->redis->hGet($key, $hash_key);

        if ($post_id >= $curr_post_id) {
            return $this->redis->hSet($key, $hash_key, $post_id);
        }

        return false;
    }

    /**
     * 获取用户的未读消息状态
     * @param $user_id
     * @return array
     */
    public function get_user_unread($user_id)
    {
        $key = USER_INFO_PREFIX.$user_id;
        $brand_unread_status = 0;
        $share_unread_status = 0;
        $help_unread_status  = 0;

        $brand = 100;
        $share = 1;
        $help  = '2_3';

        $brand_key = 'category_'.$brand;    // 品牌
        $share_key = 'category_'.$share;    // 分享
        $help_key  = 'category_'.$help;     // 求助

        // 获取用户 当前已读 分类下的最大帖子ID
        //$curr_brand = (int)$this->redis->hGet($key, $brand_key);
        $curr_share = (int)$this->redis->hGet($key, $share_key);
        $curr_help  = (int)$this->redis->hGet($key, $help_key);

        $this->load->model('rssm');
        $rss_thread = $this->rssm->get_rss_thread($user_id);

        // 遍历科室
        foreach ($rss_thread as $v) {
            $max_share_key = THREAD_UNREAD_PREFIX . $v . ':' . $share;
            $max_help_key  = THREAD_UNREAD_PREFIX . $v . ':' . $help;

            //$max_brand = (int)$this->redis->get($max_brand_key);
            $max_share = (int)$this->redis->get($max_share_key);
            $max_help  = (int)$this->redis->get($max_help_key);

            if ($max_share > $curr_share) $share_unread_status = 1;
            if ($max_help  > $curr_help)  $help_unread_status  = 1;

            if ($share_unread_status && $help_unread_status) {
                break;
            }
        }

        // 遍历标签
        if (!$share_unread_status || !$help_unread_status) {
            $rss_tag = $this->rssm->get_rss_tag($user_id);

            foreach ($rss_tag as $v) {

                if ($share_unread_status && $help_unread_status) {
                    break;
                }

                //$max_brand_key = THREAD_UNREAD_PREFIX . $v .$brand;
                $max_share_key = TAG_UNREAD_PREFIX . $v .$share;
                $max_help_key  = TAG_UNREAD_PREFIX . $v .$help;

                $max_share = (int)$this->redis->get($max_share_key);
                $max_help  = (int)$this->redis->get($max_help_key);

                if ($max_share > $curr_share) $share_unread_status = 1;
                if ($max_help  > $curr_help)  $help_unread_status  = 1;
            }
        }

        //将要过期的时候设置未读提示
        if ($share_unread_status == 1) {
            $share_list_first_key = USER_RSS_PREFIX.$user_id.':'.$share.':1';
            $share_ttl = $this->redis->ttl($share_list_first_key);
            $share_unread_status = $share_ttl <= 1 ? 1 : 0;
        }

        if ($help_unread_status == 1) {
            $help = 2;
            $help_list_first_key  = USER_RSS_PREFIX.$user_id.':'.$help.':1';
            $help_ttl  = $this->redis->ttl($help_list_first_key);
            $help_unread_status = $help_ttl <= 1 ? 1 : 0;
        }

        $brand_unread_status = 0;
        return array(
            'brand_unread_status' => $brand_unread_status,  // 品牌暂时不加红点
            'share_unread_status' => $share_unread_status,
            'help_unread_status'  => $help_unread_status,
        );
    }
}